<template>
  <div class="updata-name">

    <!-- 导航栏 -->
    <van-nav-bar left-text="取消"
      right-text="完成"
      title="设置昵称"
      @click-left="$emit('close')"
      @click-right="onConfirm">

    </van-nav-bar>

    <!-- 输入框 -->
    <div class="field">
          <van-field v-model="localName"
      rows="2"
      autosize
      type="textarea"
      maxlength="7"
      placeholder="请输入留言"
      show-word-limit />
    </div>

  </div>
</template>

<script>

import { updateUserProfiled } from '@/api/user.js'
export default {
  name: 'Updata-Name',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })

      try {
        if (this.localName === '') {
          this.$toast('昵称不能为空')
          // eslint-disable-next-line no-useless-return
          return
        }

        const { data } = await updateUserProfiled({
          name: this.localName
        })
        console.log(data)

        // 更新视图
        this.$emit('input', this.localName)
        console.log(11)
        // 关闭弹层
        this.$emit('close')

        // 提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.field {
  padding: 20px;
}
</style>
